package com.kevis.zhunblog.admin.controller.modules.sys;

import com.kevis.webCommon.constant.SessionKeyConstant;
import com.kevis.webCommon.pojo.ImageCode;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

/**
 * 图片验证码控制器
 * <p>
 * kevis
 * 2023/03/22
 */
@RestController
public class SysCaptchaController {
    //public final static String SESSION_KEY_IMAGE_CODE = "SESSION_KEY_IMAGE_CODE";

    @GetMapping("/code/image")
    public void createCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //第三方的验证码
        ImageCode imageCode = createImageCode1(request, response);
        request.getSession().setAttribute(SessionKeyConstant.SESSION_KEY_IMAGE_CODE, imageCode);

        //自定义的图片验证码
//        ImageCode imageCode = createImageCode();
//        request.getSession().setAttribute(SESSION_KEY_IMAGE_CODE, imageCode);
//        ImageIO.write(imageCode.getImage(), "jpeg", response.getOutputStream());

    }

    private ImageCode createImageCode1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        int width = 100; //验证码图片宽度
        int height = 36; //验证码图片长度
        int length = 4; //验证码位数
        int expireIn = 60; //验证码有效时间 60s
        CaptchaUtil.out(width, height, length, request, response);
        //第三方的写死的 key
        String captcha = (String) request.getSession().getAttribute("captcha");
        return new ImageCode(null, captcha, expireIn);
    }

    private ImageCode createImageCode() {
        int width = 100; //验证码图片宽度
        int height = 36; //验证码图片长度
        int length = 6; //验证码位数
        int expireIn = 60; //验证码有效时间 60s

        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics g = image.getGraphics();
        Random random = new Random();
        //填充整个背景
        g.setColor(getRandColor(200, 250));
        g.fillRect(0, 0, width, height);
        //在背景上画多条线条进行干扰
        g.setFont(new Font("Times New Roman", Font.ITALIC, 20));
        g.setColor(getRandColor(160, 200));
        for (int i = 0; i < 155; i++) {
            int x = random.nextInt(width);
            int y = random.nextInt(height);
            int x1 = random.nextInt(12);
            int y1 = random.nextInt(12);
            g.drawLine(x, y, x + x1, y + y1);
        }

        StringBuilder sRand = new StringBuilder();
        for (int i = 0; i < length; i++) {
            //取数值[0,10)
            String rand = String.valueOf(random.nextInt(10));
            sRand.append(rand);
            g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
            g.drawString(rand, 13 * i + 6, 16);
        }

        g.dispose();

        return new ImageCode(image, sRand.toString(), expireIn);

    }

    /**
     * 随机获取颜色的值
     *
     * @param fc
     * @param bc
     * @return
     */
    private Color getRandColor(int fc, int bc) {
        Random random = new Random();
        if (fc > 255) fc = 255;
        if (bc > 255) bc = 255;

        int r = fc + random.nextInt(bc - fc + 1);//参数 bound 必须大于0,范围是在[0,N),包括0但不包含n
        int g = fc + random.nextInt(bc - fc + 1);
        int b = fc + random.nextInt(bc - fc + 1);
        return new Color(r, g, b);
    }
}
